<!doctype html>
{% extends "base.html" %}
{% block title %}防火墙{% endblock title %}

{% block page2_show %}show{% endblock page2_show %}
{% block page2.5_active %}active{% endblock page2.5_active %}


{% block main_content %}
<div class="container-fluid p-0">

    <a href="/refreshFW" class="btn btn-primary">立即更新</a>
    <a>{{ message }}</a>
    <div class="row">
        {% for hostname,hostdata in firewalls.items %}
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title">{{ hostname }} FireWall列表</h5>
                    <h6 class="card-subtitle text-muted">列表为"firewall-cmd --list-ports"的结果</h6>
                </div>
                <div class="card-body">
                    <table id="datatables-multi_{{ hostname }}_firewall" class="table table-hover" style="width:100%">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>协议</th>
                                <th>端口</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        
                        <tbody>
                            {% for id,item in hostdata.data.items %}
                            <tr>
                                <td>{{ id }}</td>
                                <td>{{ item.protocol }}</td>
                                <td>{{ item.port }}</td>
                                <td>{% if item.type == "ACCEPT" %}
                                        允许
                                    {% else %}
                                        禁止
                                    {% endif %}
                                </td>
                                <td>
                                    {% if forloop.counter == 1 %}
                                        <a href="" data-toggle="modal" data-target="#add_{{ hostname }}"><i class="fe fe-plus-circle"></i></a>
                                    {% endif %}
                                    <a href="" data-toggle="modal" data-target="#del_{{ hostname }}_{{ id }}"><i class="fe fe-trash float-right"></i></a>
                                </td>
                                {% comment %} 删除FireWall的表单 {% endcomment %}
                                    <div class="modal fade" id="del_{{ hostname }}_{{ id }}" tabindex="-1" role="dialog" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h1 class="modal-title">是否删除 No.{{ id }}</h1>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                      </button>
                                                </div>
                                                <form method="post" action="/FWDel">
                                                <div class="modal-body m-3">
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-sm-4 text-sm-right">ID</label>
                                                        <div class="col-sm-8">
                                                            <input type="number" name="id" class="form-control" readonly="readonly" value="{{ id }}">
                                                        </div>
                                                    </div>
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-sm-4 text-sm-right">所属主机</label>
                                                        <div class="col-sm-8">
                                                            <input type="text" name="hostname" value="{{ hostname }}" readonly="readonly" class="form-control">
                                                        </div>
                                                    </div>
                                                    <p class="mb-0">删除后无法恢复，请慎重，点击 弹窗外/X按钮/取消按钮 可取消</p>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                                                    <button type="submit" class="btn btn-danger">删除</button>
                                                </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                {% comment %} 新增FireWall的表单 {% endcomment %}
                                    <div class="modal fade" id="add_{{ hostname }}" tabindex="-1" role="dialog" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h1 class="modal-title">新增规则至 {{ hostname }}</h1>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                      </button>
                                                </div>
                                                <form method="post" action="/FWAdd">
                                                <div class="modal-body m-2">
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-sm-3 text-sm-right">所属主机</label>
                                                        <div class="col-sm-8">
                                                            <input type="text" name="hostname" value="{{ hostname }}" readonly="readonly" class="form-control">
                                                        </div>
                                                    </div>
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-sm-3 text-sm-right">端口</label>
                                                        <div class="col-sm-8">
                                                            <input type="number" name="port" required="" class="form-control">
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <label class="col-form-label col-sm-3 text-sm-right pt-sm-0">协议</label>
                                                        <div class="col-sm-8">
                                                            <select class="custom-select mb-3" name="protocol">
                                                                <option value="tcp">TCP</option>
                                                                <option value="udp">UDP</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <label class="col-form-label col-sm-3 text-sm-right pt-sm-0">是否允许</label>
                                                        <div class="col-sm-8">
                                                            <select class="custom-select mb-3" name="type">
                                                                <option value="ACCEPT">允许</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <label class="col-form-label col-sm-3 text-sm-right pt-sm-0">是否永久生效</label>
                                                        <div class="col-sm-8">
                                                            <select class="custom-select mb-3" name="params">
                                                                <option value="--permanent">永久</option>
                                                                <option value="">临时(下一次重启失效)</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-primary">确定</button>
                                                </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
{% endblock main_content %}


{% block script %}
<script src="/assets/libs/datatables/datatables.min.js"></script>
<script>

    {% for hostname in firewalls %}
    var datatablesMulti = $("#datatables-multi_{{ hostname }}_firewall").DataTable({
        language: { // ajax 获取语言文件
            "url": "assets/libs/datatables/Chinese.json"
        },
        select: {
            style: "multi"
        }
    });
    {% endfor %}

</script>
{% endblock script %}